refract_time=50;
min_dur=10;

l_win=1000;
r_win=5000;

dr_snips=zeros(l_win+r_win+1,1100);
dr_snips_cnt=0;
do_layer=2;

stnd_dx=nan(length(AV_data),1);
stnd_dy=nan(length(AV_data),1);

for ind=1:length(AV_data)
    if AV_data(ind).layer==do_layer & AV_data(ind).ACode==1
        ind
        
        %dr=[(sqrt(AV_data(ind).dx.^2 + AV_data(ind).dy.^2))];
        dr=AV_data(ind).dx;
        stnd_dx(ind)=std(AV_data(ind).dx-mean(AV_data(ind).dx));
        stnd_dy(ind)=std(AV_data(ind).dy-mean(AV_data(ind).dy));
        
        [dr_ms]=map_ROI_act_to_aux_data(dr,AV_data(ind).frame_times,length(AV_data(ind).PS));
        
        [ps_raw]=find_ps_during_running(AV_data(ind).velP_ind,AV_data(ind).velM_ind);
        [ps_start]=parse_cond_starts(ps_raw,min_dur,refract_time,1);
        [mo_start]=parse_cond_starts_vs(AV_data(ind).velP_ind,min_dur,refract_time,1);
        %mo_start=ps_start;
        mo_start=mo_start(mo_start>AV_data(ind).frame_times(1)+l_win);
        for jnd=1:length(mo_start)
            try
                dr_snips_cnt=dr_snips_cnt+1;
                dr_snips(:,dr_snips_cnt)=dr_ms(mo_start(jnd)-l_win:mo_start(jnd)+r_win);
            end
        end
    end
end

figure;
plot([-l_win:r_win-1],smooth(diff(mean(dr_snips(:,1:dr_snips_cnt),2)),100),'k','linewidth',2)
set(gca,'xticklabel',[-1:5])
set(gca,'xtick',[-1000:1000:5000])
set(gca,'ytick',[-2:2:8]*1e-3)
set(gca,'yticklabel',[-2:2:8])
box off
ylabel('Radial brain velocity [um/s]')
xlabel('Time [s]')
ylim([-3 8]*1e-3)
xlim([-l_win+10 r_win-10])

figure;
plot([-l_win:r_win],((mean(dr_snips(:,1:dr_snips_cnt),2))),'k','linewidth',2)
